package com.ddone.finance.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ddone.common.exception.BusinessException;
import com.ddone.common.result.ResponseEnum;
import com.ddone.finance.Listener.DictDtoListener;
import com.ddone.finance.pojo.dto.DictExcelDto;
import com.ddone.finance.pojo.entity.Dict;
import com.ddone.finance.mapper.DictMapper;
import com.ddone.finance.service.DictService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;

/**
 * <p>
 * 数据字典 服务实现类
 * </p>
 *
 * @author ddone
 * @since 2024-04-10
 */
@Service
@Slf4j
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {

    @Resource
    private DictMapper dictMapper;

    @Override
    public void importExcel(InputStream fileStream) {
        try {
            EasyExcel.read(fileStream)
                    .head(DictExcelDto.class)
                    .registerReadListener(new DictDtoListener(this))
                    .sheet()
                    .doRead();
            log.info("文件读取成功");
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(ResponseEnum.IMPORT_DATA_ERROR);
        }
    }

    @Override
    public List<Dict> listByPatentId(Long parentId){
        List<Dict> dictList = dictMapper.selectList(Wrappers.lambdaQuery(Dict.class).eq(Dict::getParentId, parentId));
        dictList.forEach(dict -> dict.setHasChildren(this.hasChildren(dict.getId())));
        return dictList;
    }

    @Override
    public boolean hasChildren(Long id){
        Integer count = dictMapper.selectCount(Wrappers.lambdaQuery(Dict.class).eq(Dict::getParentId, id));
        return count > 0;
    }

    @Override
    public List<Dict> findByDictCode(String dictCode) {
        LambdaQueryWrapper<Dict> wrapper = Wrappers.lambdaQuery(Dict.class);
        wrapper.eq(Dict::getDictCode, dictCode);
        Dict dict = baseMapper.selectOne(wrapper);
        return listByPatentId(dict.getId());
    }

    @Override
    public String getNameByParentDictCodeAndValue(String dictCode, Integer value) {
        QueryWrapper<Dict> dictQueryWrapper = new QueryWrapper<Dict>();
        dictQueryWrapper.eq("dict_code", dictCode);
        Dict parentDict = baseMapper.selectOne(dictQueryWrapper);

        if(parentDict == null) {
            return "";
        }

        dictQueryWrapper = new QueryWrapper<>();
        dictQueryWrapper
                .eq("parent_id", parentDict.getId())
                .eq("value", value);
        Dict dict = baseMapper.selectOne(dictQueryWrapper);

        if(dict == null) {
            return "";
        }

        return dict.getName();
    }
}
